package com.star.mall.persistence.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.mall.constants.KeyConstant;
import com.star.mall.persistence.entity.Menu;
import com.star.mall.persistence.entity.Role;
import com.star.mall.persistence.mapper.MenuMapper;
import com.star.mall.persistence.service.IMenuService;
import com.star.mall.persistence.service.IRoleAuthService;
import com.star.mall.persistence.service.IRoleService;
import com.star.mall.utils.ContextUtil;
import com.star.mall.utils.TreeUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表  服务实现类
 * </p>
 *
 * @author StarQ
 * @since 2021-03-14
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    IRoleService roleService;
    @Resource
    IRoleAuthService roleAuthService;

    @Override
    public List<Menu> getCurrentMenus() {
        return getMenusByUserId(ContextUtil.getCurrentUserId());
    }

    @Override
    @Cacheable(value = KeyConstant.MENU_CACHE_NAME, key = "#userId")
    public List<Menu> getMenusByUserId(String userId) {
        if (ContextUtil.getCurrentUser().isAdmin()) {
            return TreeUtil.listToTree(list());
        }
        List<Role> roles = roleService.getRolesByUserId(userId);
        Set<String> roleIds = roles.stream().map(Role::getId).collect(Collectors.toSet());
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        return TreeUtil.listToTree(getMenusByRoleIds(roleIds));
    }

    @Override
    public List<Menu> getMenusByAlias(Set<String> alias) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.in("alias_", alias);
        wrapper.eq("enable_", Menu.ENABLE);
        return list(wrapper);
    }

    @Override
    public List<Menu> getMenusByRoleIds(Set<String> roleIds) {
        Set<String> authKeys = roleAuthService.getAuthKeyByRoleIds(roleIds);
        if (authKeys.isEmpty()) {
            return new ArrayList<>();
        }
        return getMenusByAlias(authKeys);
    }

    @Override
    @CacheEvict(value = KeyConstant.MENU_CACHE_NAME, key = "1")
    public boolean save(Menu menu) {
        return super.save(menu);
    }

    @Override
    @CacheEvict(value = KeyConstant.MENU_CACHE_NAME, key = "1")
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @Override
    @CacheEvict(value = KeyConstant.MENU_CACHE_NAME, allEntries = true)
    public boolean updateById(Menu menu) {
        return super.updateById(menu);
    }

}
